package xiaohao.order.dao.impl;

import static com.google.common.base.Preconditions.*;

import com.taobao.api.ApiException;
import com.taobao.api.domain.Shipping;
import com.taobao.api.request.LogisticsOrdersGetRequest;
import com.taobao.api.response.LogisticsOrdersGetResponse;

import xiaohao.order.dao.LogisticsDao;
import xiaohao.order.top.TopDaoSupport;

public class DefaultLogisticsDao extends TopDaoSupport implements LogisticsDao {

	@Override
	public Shipping getShippingByTid(long tid, String sessionKey) {
		checkArgument(tid > 0);
		checkNotNull(sessionKey);
		
		LogisticsOrdersGetRequest request = new LogisticsOrdersGetRequest();
		request.setTid(tid);
		request.setFields("tid, freight_payer, type, company_name");
		
		LogisticsOrdersGetResponse response = null;
		try {
			response = getTaobaoClient().execute(request, sessionKey);
		} catch (ApiException e) {
			log.warn(e);
			return null;
		}
		
		if (!response.isSuccess()) {
			return null;
		}
		
		return response.getShippings().get(0);
	}

}
